package com.oscar.practice.shiro.biz.shiro;

import org.apache.shiro.crypto.RandomNumberGenerator;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.oscar.practice.shiro.model.user.UserInfo;

/**
 * 用户密码
 *
 * @author tongyufu
 *
 */
@Service
public class PasswordBiz {

    private RandomNumberGenerator randomNumberGenerator = new SecureRandomNumberGenerator();
    @Value("${shiro.password.algorithmName}")
    private String                algorithmName         = "md5";
    @Value("${shiro.password.hashIterations}")
    private int                   hashIterations        = 2;

    /**密码加密*/
    public void encryptPassword(UserInfo user) {
        user.setSalt(randomNumberGenerator.nextBytes().toHex());
        String password = new SimpleHash(algorithmName, user.getPassword(), user.getSalt(),
            hashIterations).toHex();
        user.setPassword(password);
    }

    /**密码加密*/
    public String encryptPassword(String password, String salt) {
        return new SimpleHash(algorithmName, password, salt, hashIterations).toHex();
    }

}
